
package com.ps.helpdesk.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ps.helpdesk.business.BusinessTipoocorrencia;
import com.ps.helpdesk.model.Tipoocorrencia;
import com.ps.helpdesk.persistence.ManagerConnection;

/**
 * Servlet implementation class for Servlet: FormTipoocorrencia
 * 
 */
public class FormTipoocorrencia extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	static final long serialVersionUID = 1L;

	public FormTipoocorrencia() {

		super();
		
		ManagerConnection.DATA_BASE = "helpdesk";
	}

	protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		doPost( request, response );
	}

	protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		if ( request.getParameter( "edit" ) != null 
				&& String.valueOf( request.getParameter( "edit" ) ).trim().length() > 0 ) {
			find( request, response );
		}
		else if ( request.getParameter( "delete" ) != null 
				&& String.valueOf( request.getParameter( "delete" ) ).trim().length() > 0 ) {
			delete( request, response );
		}
		else if ( "inserir".equals( request.getParameter( "submit" ) ) 
				|| "alterar".equals( request.getParameter( "submit" ) ) ) {
			persist( request, response );
		}
		else if ( "novo".equals( request.getParameter( "submit" ) ) ) {
			send( request, response, "?idtipoocorrencia=&descricao="  );
		}
		else {
			send( request, response, "" );
		}
	}
	
	private void loadSituacoes( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
		
		BusinessTipoocorrencia bs = new BusinessTipoocorrencia();
		List<Tipoocorrencia> tipoocorrencias = bs.findAll();

		if ( request.getAttribute( "lista" ) == null ) {
			request.setAttribute( "lista", tipoocorrencias );
		}
		else {
			((List<Tipoocorrencia>)request.getAttribute( "lista" )).addAll( tipoocorrencias );
		}
	}
	
	private void send( HttpServletRequest request, HttpServletResponse response, String parameters ) throws ServletException, IOException {
		
		loadSituacoes( request, response );
		getServletContext().getRequestDispatcher( "/pages/admin/tipoocorrencia.jsp" + parameters ).forward( request, response );
	}
	
	private void sendError( HttpServletRequest request, HttpServletResponse response, BusinessTipoocorrencia bs ) throws ServletException, IOException {
		
		if ( bs != null ) {
			request.setAttribute( "mensagem", bs.getMensagem() );
		}
	
		getServletContext().getRequestDispatcher( 
				"/pages/erropage.jsp?link=/helpdesk/FormTipoocorrencia" )
					.forward( request, response );
	}

	private void find( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessTipoocorrencia bs = new BusinessTipoocorrencia();
		Tipoocorrencia tipoocorrencia = bs.find( request.getParameter( "edit" ) );

		if ( tipoocorrencia != null ) {
			send( request, response, 
					"?idtipoocorrencia=" + tipoocorrencia.getIdtipoocorrencia() + 
					"&descricao=" + tipoocorrencia.getDescricao() );
		}
		else {
			sendError( request, response, bs );
		}
	}

	private void persist( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessTipoocorrencia bs = new BusinessTipoocorrencia();
		
		Tipoocorrencia tipoocorrencia = new Tipoocorrencia();
		tipoocorrencia.setIdtipoocorrencia( Integer.parseInt( request.getParameter( "idtipoocorrencia" ).trim() ) );
		tipoocorrencia.setDescricao( request.getParameter( "descricao" ).trim() );
		
		if ( bs.persist( tipoocorrencia ) ) {
			send( request, response,  
					"?idtipoocorrencia=" + tipoocorrencia.getIdtipoocorrencia() + 
					"&descricao=" + tipoocorrencia.getDescricao() );
		}
		else {
			sendError( request, response, bs );
		}
	}

	private void delete( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessTipoocorrencia bs = new BusinessTipoocorrencia();		
		Integer key = Integer.parseInt( request.getParameter( "delete" ).trim() );
		
		if ( bs.remove( key ) ) {
			send( request, response, "?idtipoocorrencia=&descricao=" );
		}
		else {
			sendError( request, response, bs );
		}
	}
}